package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.Notice;
import com.wzq.dao.pojo.PageBean;
import com.wzq.dao.pojo.R;
import com.wzq.dao.pojo.Sysnumber;
import com.wzq.service.INoticeService;
import com.wzq.service.ISysnumberService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/notice")
public class AdminNoticeController {

    @Autowired
    INoticeService iNoticeService;
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<Notice> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Notice> pageResult = iNoticeService.page(page, new QueryWrapper<Notice>().like(StringUtil.isNotEmpty(query), "number", query));
        Map<String,Object> map=new HashMap<>();
        map.put("NoticeList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

    @RequestMapping("/save")
    public R save(@RequestBody Notice notice){
        if(notice.getId()==null || notice.getId()==-1){
            iNoticeService.save(notice);
        }else{
            iNoticeService.saveOrUpdate(notice);
        }
        return R.ok();
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        Notice notice = iNoticeService.getById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("notice",notice);
        return R.ok(map);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id){
        iNoticeService.removeById(id);
            return R.ok();
        }
}